﻿Imports System.Text.RegularExpressions

Public Class 商品出库

    Private database As DataAccess

    Private data_Good As DataTable
    Private data_Out As DataTable

    Private good_id As Integer
    Private good_type As String
    Private good_name As String
    Private total_number As Integer
    Private unit As String
    Private wholesale_price As String
    Private retail_price As String

    Private Sub 商品出库_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        database = New DataAccess
        database.OpenConnection()

        Dim sqlString As String
        sqlString = "select * from tf_f_goods"
        data_Good = database.QueryDatabase(sqlString)

        sqlString = "select * from tf_out_goods"
        data_Out = database.QueryDatabase(sqlString)

        '初始化商品类型和商品名称列表
        GoodTypeList_Init()
        GoodNameList_Init()

        '读取商品销售信息(库存, 销售单价)
        'good_info_get()

    End Sub

    Private Sub GoodTypeList_Init()

        ComboBox_Good_Type.Items.Clear()

        '将商品类型数据列在下拉菜单中
        Dim row As DataRow
        For Each row In data_Good.Rows
            good_type = row("goods_type")

            ' 去除重复项
            If ComboBox_Good_Type.FindString(good_type) = -1 Then
                ComboBox_Good_Type.Items.Add(good_type)
            End If

        Next row

        If ComboBox_Good_Type.Items.Count > 0 Then
            ComboBox_Good_Type.SelectedIndex = 0
        End If

    End Sub

    Private Sub GoodNameList_Init()

        ComboBox_Good_Name.Items.Clear()

        '从数据库中获取商品名称信息
        good_type = ComboBox_Good_Type.Text

        Dim rows() As DataRow
        rows = data_Good.Select("goods_type ='" & good_type & "'")

        '将商品名称数据列在下拉菜单中
        Dim row As DataRow
        For Each row In rows
            'MessageBox.Show("data_Good.Rows2")
            ComboBox_Good_Name.Items.Add(row("goods_name"))
        Next row

        If ComboBox_Good_Name.Items.Count > 0 Then
            ComboBox_Good_Name.SelectedIndex = 0
        End If

    End Sub

    Private Sub ComboBox_Good_Type_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox_Good_Type.SelectedIndexChanged

        '更新商品名称列表
        GoodNameList_Init()

        '读取商品销售信息(库存, 销售单价)
        get_good_info()

    End Sub

    Private Sub ComboBox_Good_Name_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox_Good_Name.SelectedIndexChanged

        '读取商品销售信息(库存, 销售单价)
        get_good_info()

    End Sub

    Private Sub get_good_info()

        good_type = ComboBox_Good_Type.Text
        good_name = ComboBox_Good_Name.Text

        If good_type = "" Or good_name = "" Then
            Return
        End If

        '读取商品信息
        Dim rows() As DataRow = data_Good.Select("goods_type='" & good_type & "' and goods_name='" & good_name & "'")
        If rows.Length = 1 Then
            Dim row As DataRow
            For Each row In rows
                good_id = row.Item("goods_id")
                total_number = row.Item("num")
                wholesale_price = row.Item("wholesale_price")
                retail_price = row.Item("retail_price")
                unit = row.Item("unit")
            Next row
        ElseIf rows.Length = 0 Then
            MessageBox.Show("没有在数据库中查询到此商品")
        ElseIf rows.Length > 1 Then
            MessageBox.Show("在数据库中查询商品是出现错误")
        End If

        '初始化销售信息
        Dim number As Integer = 0
        If total_number > 0 Then
            number = 1
        End If

        Dim price As Decimal = retail_price

        If unit = "无" Then
            Label_Unit.Text = "单位"
        Else
            Label_Unit.Text = unit
        End If

        TextBox_Out_Number.Text = number
        TextBox_Price.Text = price
        TextBox_Total_Price.Text = number * price

    End Sub

    Private Sub TextBox_Out_Number_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox_Out_Number.TextChanged

        Dim number As Integer
        Dim price As Decimal

        '判断数量是否合法
        If check_input_string(TextBox_Out_Number.Text) = False Then
            Return
        End If

        '判断数量是否超过商品总数
        If TextBox_Out_Number.Text > total_number Then
            MessageBox.Show("错误: 最大商品数量为 " & total_number)
            TextBox_Out_Number.Text = 0
            Return
        End If

        number = TextBox_Out_Number.Text

        '更新商品价格
        '   如果销售数额小于10, 使用零售价格
        '   如果销售数额大于(等于)10, 使用批发价格
        If number >= 10 Then
            TextBox_Price.Text = wholesale_price
        Else
            TextBox_Price.Text = retail_price
        End If

        price = TextBox_Price.Text
        TextBox_Total_Price.Text = number * price

    End Sub

    Private Function check_input_string(ByVal str As String) As Boolean

        '    是否为空
        '    是否有非法字符

        If Regex.Match(str, "( )+").Success = True Then
            Return False
        End If

        If str = "" Then
            Return False
        End If

        Return True
    End Function

    Private Sub Button_Out_Good_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Out_Good.Click

        '检查输入是否合法
        If check_input_string(TextBox_Out_Number.Text) = False Then
            Return
        End If

        If check_input_string(TextBox_Total_Price.Text) = False Then
            Return
        End If

        If TextBox_Out_Number.Text = "0" Or _
            TextBox_Total_Price.Text = "0" Then
            MessageBox.Show("错误: 销售数量为0, 或者总销售价格为0")
        End If

        If CheckBox_Good_Cancel.Checked = True And _
            CheckBox_Owing.Checked = True Then

            MessageBox.Show("退货和欠账不能同时选上")

        End If

        Dim number As Integer = TextBox_Out_Number.Text
        Dim price As Decimal = TextBox_Price.Text
        Dim total_price As Decimal = TextBox_Total_Price.Text
        Dim remark As String = TextBox_Remark.Text

        '退货操作
        If CheckBox_Good_Cancel.Checked = True Then
            If good_cancel(good_type, good_name, number, unit, price, total_price) = False Then
                MessageBox.Show("商品退货失败")
            Else
                MessageBox.Show("商品退货成功")
            End If
            Return
        End If

        '销售商品
        Dim good_owing As Integer = 0
        If CheckBox_Owing.Checked = True Then
            good_owing = 1
        End If

        If good_sell(good_type, good_name, number, unit, price, total_price, good_owing, remark) = False Then
            MessageBox.Show("商品销售失败")
        Else
            MessageBox.Show("商品销售成功")
        End If

    End Sub

    Private Function good_cancel(ByVal good_type As String, ByVal good_name As String, _
                                 ByVal number As Integer, ByVal unit As String, _
                                 ByVal price As Decimal, ByVal total_price As Decimal) As Boolean

        Return True
    End Function

    Private Function good_sell(ByVal good_type As String, ByVal good_name As String, _
                               ByVal number As Integer, ByVal unit As String, _
                               ByVal price As Decimal, ByVal total_price As Decimal, _
                               ByVal owing_tag As Integer, ByVal remark As String) As Boolean

        '获取出库批次号
        Dim subscribe_id As Integer = get_max_subscribe_id() + 1

        '更新出库表
        database.SetSelectCommand("select * from tf_out_goods")

        Dim row As DataRow
        row = data_Out.NewRow
        data_Out.Rows.Add(row)

        row("subscribe_id") = subscribe_id
        row("goods_id") = good_id
        row("goods_type") = good_type
        row("goods_name") = good_name
        row("out_price") = CSng(price)
        row("num") = CInt(number)
        row("unit") = unit
        row("owing_tag") = owing_tag
        row("cancel_tag") = 0
        row("out_time") = System.DateTime.Now.ToString
        row("update_time") = System.DateTime.Now.ToString
        row("remark") = remark

        If database.UpdateDatabase(data_Out) = False Then
            MessageBox.Show("更新商品入库数据库时失败")
            Return False
        End If

        '  更新商品实体表
        database.SetSelectCommand("select * from tf_f_goods")

        For Each row In data_Good.Rows
            If good_id = row.Item("goods_id") Then
                row("goods_id") = good_id
                row("num") = row("num") - number
            End If
        Next row

        If database.UpdateDatabase(data_Good) = False Then
            MessageBox.Show("更新商品出库数据库时失败")

            ' To Do
            ' 删除在出库表中添加的数据

            Return False
        End If

        Return True
    End Function

    Private Function get_max_subscribe_id() As Integer

        Dim max_subscribe_id As Integer = 0

        Dim row As DataRow
        For Each row In data_Out.Rows
            If max_subscribe_id <= row.Item("subscribe_id") Then
                max_subscribe_id = row.Item("subscribe_id")
            End If
        Next row

        Return max_subscribe_id
    End Function

End Class